package com.liutaw.nanjingair.views.viewcomponent;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.liutaw.nanjingair.R;

import java.io.File;

public class ViewUtils {
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static ObjectAnimator nope(View view) {
        int delta = view.getResources().getDimensionPixelOffset(R.dimen.spacing_medium);

        PropertyValuesHolder pvhTranslateX = PropertyValuesHolder.ofKeyframe(View.TRANSLATION_X,
                Keyframe.ofFloat(0f, 0),
                Keyframe.ofFloat(.10f, -delta),
                Keyframe.ofFloat(.26f, delta),
                Keyframe.ofFloat(.42f, -delta),
                Keyframe.ofFloat(.58f, delta),
                Keyframe.ofFloat(.74f, -delta),
                Keyframe.ofFloat(.90f, delta),
                Keyframe.ofFloat(1f, 0f)
        );

        return ObjectAnimator.ofPropertyValuesHolder(view, pvhTranslateX).
                setDuration(500);
    }

//    public static void buildDialog(Context context, final View.OnClickListener comfirmListener, final View.OnClickListener dismissListener) {
//        ViewHolder holder = new ViewHolder(R.layout.dialog_content);
//        final DialogPlus dialogPlus = DialogPlus.newDialog(context).setCancelable(true).setContentHolder(holder).create();
//        View view = holder.getInflatedView();
//        Button comdirm = (Button) view.findViewById(R.id.button_comfirm_dialog);
//
//        comdirm.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialogPlus.dismiss();
//                comfirmListener.onClick(v);
//            }
//        });
//        final Button dismiss = (Button) view.findViewById(R.id.button_cancel_dialog);
//
//        dismiss.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dialogPlus.dismiss();
//                if (dismissListener != null)
//                    dismissListener.onClick(v);
//            }
//        });
//
//        dialogPlus.show();
//    }

    public static void closeKeyboard(Context context, EditText etName) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //关闭软键盘
        imm.hideSoftInputFromWindow(etName.getWindowToken(), 0);
    }

    public static void showDialogMessage(Activity activity, String message) {
        new AlertView(null, message, null, new String[]{"好"}, null, activity, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
            }
        }).show();
    }

    public static void showDialogMessage(Activity activity, String message, final View.OnClickListener onClickListener) {
        new AlertView(null, message, null, new String[]{"好"}, null, activity, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

            }
        }).setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(Object o) {
                if (onClickListener==null)
                    return;
                onClickListener.onClick(null);
            }
        }).show();
    }

    public static void showDialogMessage(Activity activity, String message, String buttonInfo, final View.OnClickListener onClickListener) {
        new AlertView(null, message, null, new String[]{"取消", buttonInfo}, null, activity, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                switch (position) {
                    case 0:
                        break;
                    case 1:
                        onClickListener.onClick(null);
                        break;
                }
            }
        }).show();
    }

    public static void showDialogMessageWithRetry(Activity activity, String message, final View.OnClickListener onClickListener) {
        new AlertView(null, message, null, new String[]{"重试"}, null, activity, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                onClickListener.onClick(null);
            }
        }).show();
    }

    public static void showDialogMessageWithCancel(Activity activity, String message, final View.OnClickListener onClickListener) {
        new AlertView(null, message, null, new String[]{"好", "取消"}, null, activity, AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                switch (position) {
                    case 0:
                        onClickListener.onClick(null);
                        break;
                    case 1:
                        break;
                }
            }
        }).show();
    }

    public static void showDialogMessage(final Activity activity, final String phoneNum, String message) {
        ViewUtils.showDialogMessageWithCancel(activity, message, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
                activity.startActivity(intent);
//                if (Build.VERSION.SDK_INT < 23) {
//
//                } else {
//                    if (ActivityCompat.checkSelfPermission(activity,
//                            Manifest.permission.CALL_PHONE)
//                            != PackageManager.PERMISSION_GRANTED) {
//
//                        // Should we show an explanation?
//                        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
//                                Manifest.permission.CALL_PHONE)) {
//
//                            // Show an expanation to the user *asynchronously* -- don't block
//                            // this thread waiting for the user's response! After the user
//                            // sees the explanation, try again to request the permission.
//                            ViewUtils.showDialogMessage(activity, "安卓6.0以上系统请授予拨号权限", new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    ActivityCompat.requestPermissions(activity,
//                                            new String[]{Manifest.permission.READ_CONTACTS},
//                                            101);
//                                }
//                            });
//                            return;
//
//                        }
//                        ActivityCompat.requestPermissions(activity,
//                                new String[]{Manifest.permission.READ_CONTACTS},
//                                101);
//                    } else {
//                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
//                        activity.startActivity(intent);
//                    }
//
//                }
            }
        });
    }

//    public static boolean judgeIfisLogin(Activity activity) {
//        if (!UserSession.getInstance().canWeContinue()) {
//            SimpleMessageDialogPlus.Builder builder = new SimpleMessageDialogPlus.Builder(activity);
//            builder.buildMessageText("您还没有登录", "登录后可以使用所有功能").buildComfirmButtonText("登录/注册", new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    FragmentProfile.goToLogin();
//                }
//            });
//            builder.build().show();
//            return false;
//        }
//        return true;
//    }
//
//    public static DialogPlus getCunstomDialogPlus(Activity activity, int resid) {
//        View view = LayoutInflater.from(activity).inflate(resid, null);
//        ViewHolder mViewHolder = new ViewHolder(view);
//        final DialogPlus mDialogPlus = DialogPlus.newDialog(activity).setCancelable(true).setContentHolder(mViewHolder).setGravity(Gravity.CENTER).create();
//        return mDialogPlus;
//    }

    public static AlertView showTwoLineMessage(Activity activity, String title, String choice1, String choice2, final View.OnClickListener choice1Listener, final View.OnClickListener choice2Listener) {
        final AlertView alertView = new AlertView(title, null, "取消", null,
                new String[]{choice1, choice2},
                activity, AlertView.Style.ActionSheet, new OnItemClickListener() {
            public void onItemClick(Object o, int position) {
                switch (position) {
                    case 0:
                        choice1Listener.onClick(null);
                        break;
                    case 1:
                        choice2Listener.onClick(null);
                        break;
                }
            }
        });
        alertView.show();
        return alertView;
    }

    public static final int IMAGE_REQUEST_CODE = 2001;
    public static final int CAMERA_REQUEST_CODE = 2002;
    public static final int RESIZE_REQUEST_CODE = 2003;

//    public static AlertView showPickPhoto(final Activity activity, final String filePath) {
//        AlertView alertView = new AlertView("上传照片", null, "取消", null,
//                new String[]{"拍照", "从相册中选择"},
//                activity, AlertView.Style.ActionSheet, new OnItemClickListener() {
//            public void onItemClick(Object o, int position) {
//                switch (position) {
//                    case 0:
//                        photo(activity, filePath);
//                        break;
//                    case 1:
//                        Crop.pickImage(activity,IMAGE_REQUEST_CODE);
////                        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
////                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
////                        activity.startActivityForResult(galleryIntent, IMAGE_REQUEST_CODE);
//                        break;
//                }
//            }
//        });
//        alertView.show();
//        return alertView;
//    }


    public static void photo(Activity activity, String filePath) {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
                activity.startActivityForResult(openCameraIntent, CAMERA_REQUEST_CODE);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(activity, "获取照片失败", Toast.LENGTH_SHORT).show();
            }


        } else {
            Toast.makeText(activity, "没有储存卡", Toast.LENGTH_LONG).show();
        }
    }

}
